<?php
class TimeHandler {
	public static function getMissionDescriptionHistory($iMission) {
		try {
			$oQuery = DataHandler::query("
				SELECT DISTINCT description
				FROM work
				WHERE mission_id = ?
				AND user_id = ?
				AND description != ''
				ORDER BY timing DESC
			", $iMission, $_SESSION['rid']);
			
			$sDescriptions = array();
			while (list($sDescription) = $oQuery->fetch(PDO::FETCH_NUM)) {
				array_push($sDescriptions, $sDescription);
			}
			return $sDescriptions;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	/* To kill? 2011-12-26
	public static function getClientDescriptionHistory($iClient) {
		try {
			$oClients = ClientHandler::getClients();
			$oQuery = DataHandler("
				SELECT DISTINCT description
				FROM work
				WHERE worknumber = ?
				AND user_id = ?
				ORDER BY timing DESC
			", $oClients[$iClient]->sSerial, $_SESSION['rid']);
			
			$sDescriptions = array();
			while (list($sDescription) = $oQuery->fetch(PDO::FETCH_NUM)) {
				array_push($sDescriptions, $sDescription);
			}
			return $sDescriptions;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	*/
	
	/* To kill? 2011-12-26
	public static function getDefaults()
	{
		try {
			if (is_array($GLOBALS['time']['default_serials'])) {
				return $GLOBALS['time']['default_serials'];
			}
			
			$oQuery = DataHandler::query("
				SELECT
					mission_id,
					serial
				FROM clients_defaults
				WHERE user_id = ?
			", $_SESSION['rid']);
			
			$sDefaults = array();
			while (list($iMission, $sSerial) = $oQuery->fetch(PDO::FETCH_NUM))
			{
				$sDefaults[$iMission] = $sSerial;
			}
			
			$GLOBALS['time']['default_serials'] = $sDefaults;
			
			return $GLOBALS['time']['default_serials'];
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	*/
	
	public static function setDefault($sSerial, $iMission)
	{
		try {
			$oCheckQuery = DataHandler::query("
				SELECT serial
				FROM clients_defaults
				WHERE mission_id = ?
				AND user_id = ?
			", $iMission, $_SESSION['rid']);

			if ($oCheckQuery->rowCount() > 0)
			{
				DataHandler::query("
					UPDATE clients_defaults
					SET serial = ?
					WHERE mission_id = ?
					AND user_id = ?
				", $sSerial, $iMission, $_SESSION['rid']);
			}
			else
			{
				DataHandler::query("
					INSERT INTO clients_defaults (
						user_id,
						serial,
						mission_id
					) VALUES (
						?,
						?,
						?
					)
				", $_SESSION['rid'], $sSerial, $iMission);
			}

			return true;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}

	public static function sendTime($iUser, $iTime)
	{
		try {
			DataHandler::query("
				UPDATE work
				SET sent = '1'
				WHERE id = ?
				AND user_id = ?
			", $iTime, $iUser);
			
			return true;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function sendMonth($iUser, $iYear, $iMonth)
	{
		try {
			$oUsers = UserHandler::getUsers();
			$oTime = TimeHandler::getTimes($_SESSION['rid'], $iMonth, $iYear);
			
			foreach ($oTime as $oThisWork)
			{
				if (!$oThisWork->sSerial || $oThisWork->iSent != 0)
				{
					continue;
				}
				
				DataHandler::query("
					UPDATE work
					SET sent = '1'
					WHERE id = ?
					AND user_id = ?
				", $oThisWork->iID, $iUser);
			}
			
			return true;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}

	public static function addTime($iMissionID, $iJob, $iStartTime, $iDuration, $sSerial, $sDescription, $bOvertime, $bDriving, $iClientgroup)
	{
		try {
			if ($iStartTime > (thisSecond() + 86400)) {
				throw new MiJobException('Can\'t write hours further than 24 hours into the future.');
			}
			
			DataHandler::query("
				INSERT INTO work (
					user_id,
					mission_id,
					starttime,
					duration,
					timing,
					worknumber,
					description,
					overtime,
					driving,
					job_id
				) VALUES (?,?,?,?,?,?,?,?,?,?)
			",
				$_SESSION['rid'],
				$iMissionID,
				$iStartTime,
				$iDuration,
				thisSecond(),
				$sSerial,
				$sDescription,
				boolize($bOvertime),
				boolize($bDriving),
				$iJob
			);
			
			TimeHandler::setDefault($sSerial, $iMissionID);
			
			MissionHandler::updateWork($iMissionID);
			
			return true;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function delTime($iTime, $iMission)
	{
		try {
			DataHandler::query("
				DELETE FROM work
				WHERE id = ?
				AND user_id = ?
			", $iTime, $_SESSION['rid']);
			
			MissionHandler::updateWork($iMission);
			
			return true;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function changeTime($iTime, $iStartTime, $iHours, $sSerial, $sDescription, $iMission, $bOvertime, $bDriving)
	{
		try {
			$iDuration = round((float)str_replace(',', '.', $iHours), 2) * 3600;
			
			DataHandler::query("
				UPDATE work
				SET
					description = ?,
					duration = ?,
					worknumber = ?,
					starttime = ?,
					overtime = ?,
					driving = ?
				WHERE id = ?
				AND user_id = ?
			", $sDescription, $iDuration, $sSerial, $iStartTime, boolize($bOvertime), boolize($bDriving), $iTime, $_SESSION['rid']);
			
			MissionHandler::updateWork($iMission);
			
			return true;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function getTimes($iUser, $iMonth = 0, $iYear = 0)
	{
		try {
			if ($iMonth)
			{
				$iYear = ($iYear ? $iYear : date('Y'));
				$iLowLimit = mktime(0, 0, 0, $iMonth, 1, $iYear);
				$iHighLimit = mktime(23, 59, 59, $iMonth, date('t', $iLowLimit), $iYear);
				$sExtraQuery = "
					AND starttime >= '$iLowLimit'
					AND starttime <= '$iHighLimit'
				";
			}
			
			$oQuery = DataHandler::query("
				SELECT
					id,
					duration,
					starttime,
					description,
					mission_id,
					worknumber,
					sent,
					job_id,
					overtime,
					driving,
					social
				FROM work
				WHERE user_id = '$iUser'
				$sExtraQuery
				ORDER BY
					starttime DESC,
					job_id,
					mission_id
			");
			
			$oTimes = array();
			while (list($iID, $iDuration, $iStartTime, $sDescription, $iMission, $sSerial, $iSent, $iJob, $bOvertime, $bDriving, $sSocial) = $oQuery->fetch(PDO::FETCH_NUM))
			{
				$oTimes[$iID] = new Time($iID, $iDuration, $iStartTime, $sDescription, $iMission, $sSerial, $iSent, $iJob, $bOvertime, $bDriving, $sSocial);
			}
			return $oTimes;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
}
?>
